/**
 * created by 小卷毛, 2018/12/19
 * Copyright (c) 2018, 416143467@qq.com All Rights Reserved.
 * #                   *********                            #
 * #                  ************                          #
 * #                  *************                         #
 * #                 **  ***********                        #
 * #                ***  ****** *****                       #
 * #                *** *******   ****                      #
 * #               ***  ********** ****                     #
 * #              ****  *********** ****                    #
 * #            *****   ***********  *****                  #
 * #           ******   *** ********   *****                #
 * #           *****   ***   ********   ******              #
 * #          ******   ***  ***********   ******            #
 * #         ******   **** **************  ******           #
 * #        *******  ********************* *******          #
 * #        *******  ******************************         #
 * #       *******  ****** ***************** *******        #
 * #       *******  ****** ****** *********   ******        #
 * #       *******    **  ******   ******     ******        #
 * #       *******        ******    *****     *****         #
 * #        ******        *****     *****     ****          #
 * #         *****        ****      *****     ***           #
 * #          *****       ***        ***      *             #
 * #            **       ****        ****                   #
 */
package com.sgcc.pda.mdrh.task.util

import android.bluetooth.BluetoothProfile
import android.content.Context
import com.sgcc.pda.bluetooth.bluetoothmanager.BlueManager
import com.sgcc.pda.frme.protocol.ws.WSFrame
import com.sgcc.pda.mdrh.task.common_ui.helper.decodeUnicode
import com.sgcc.pda.mdrh.task.common_ui.helper.hexStr2Str
import com.sgcc.pda.mdrh.task.common_ui.helper.reverse
import com.sgcc.pda.tools.LogPrintUtils

fun Context.getProfileProxy(init: _ServiceListener.() -> Unit) {
    BluetoothHelper.getAdapter()!!
        .getProfileProxy(
            this,
            _ServiceListener().apply(init),
            BluetoothHelper.getConnectedProfile()
        )
}

fun Context.sendBlueData(
    source: String,
    onSuccess: (List<String>) -> Unit = {},
    onFailed: (String) -> Unit = {}
) {
    BlueManager.getInstance(this)
        .sendData(source) {
        when (it.code) {
            0 -> {
                val frame = WSFrame.fromString(it.data)
                if (frame != null) {
                    val dataList = ArrayList<String>()

                    when (frame.function.toInt(16)) {
                        WSFrame.FN_ZYAQSB_GETSBSB -> {
                            if (frame.data.length > 2) {
                                val deviceName = frame.data.substring(2)
                                dataList.add(
                                    deviceName.reverse(2)
                                        .hexStr2Str()
                                        .decodeUnicode()
                                )
                            }

                            onSuccess(dataList)
                        }
                        WSFrame.FN_ZYAQSB_GETRWXF -> onSuccess(dataList)
                        WSFrame.FN_ZYAQSB_GETYCXX -> {
                            val data = frame.data

                            var orderReverse = data.substring(0, 40).trimEnd('0')
                            if (orderReverse.length % 2 == 1) orderReverse += "0"
                            dataList.add(orderReverse.reverse(2).hexStr2Str())
                        }
                    }
                }
                else onFailed("帧解析失败！")
            }
            else -> {
                LogPrintUtils.e("${it.code}， ${it.data}")
                onFailed(it.data)
            }
        }
    }
}

class _ServiceListener : BluetoothProfile.ServiceListener {

    private var _onServiceDisconnected: ((Int) -> Unit)? = null
    private var _onServiceConnected: ((Int, BluetoothProfile) -> Unit)? = null

    fun onServiceDisconnected(listener: (Int) -> Unit) {
        _onServiceDisconnected = listener
    }

    fun onServiceConnected(listener: (Int, BluetoothProfile) -> Unit) {
        _onServiceConnected = listener
    }

    override fun onServiceDisconnected(profile: Int) {
        _onServiceDisconnected?.invoke(profile)
    }

    override fun onServiceConnected(profile: Int, proxy: BluetoothProfile) {
        _onServiceConnected?.invoke(profile, proxy)
    }

}
